package net.worlds.object;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.PropChangeHandler;
import org.jbox2d.collision.shapes.PolygonDef;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2D;

public class Triangle extends Part{
    protected Vec2D size;
    public Triangle() throws WorldsException
    {
        super("Triangle");
        constructor();
    }
    private void constructor() throws WorldsException
    {
        addPropChangeHandler("Size", new PropChangeHandler()
        {
            @Override
            public void handle(final Object oldValue, final Object value) throws WorldsException {

                try {
                    Vec2D vec = (Vec2D) value;
                    if(vec.x < 0.125 || vec.y < 0.125)
                    {
                        System.err.println("The minimum size of a box is (0.125, 0.125)");
                        Vec2D oldVec = (Vec2D) oldValue;
                        if(oldVec != null && oldVec.x >= 0.125 && oldVec.y >= 0.125)
                            setProp("Size", oldVec, true);
                        else
                            setProp("Size", new Vec2D(0.125f, 0.125f), true);
                    }
                    else
                    {
                        if(vec.x < 3 || vec.y < 3)
                        {
                            if(inWorld)
                                body.setBullet(true);
                            else
                                bd.isBullet = true;
                        }
                        Vec2D oldCentroid = PolygonShape.computeCentroid(((PolygonDef)sd).vertices);
                        String d = getStringProp("Direction");
                        ((PolygonDef)sd).clearVertices();
                        if(d.equals("DR"))
                        {
                            ((PolygonDef)sd).addVertex(new Vec2D((float) -vec.x/2, (float) vec.y/2));
                            ((PolygonDef)sd).addVertex(new Vec2D((float) -vec.x/2, (float) -vec.y/2));
                            ((PolygonDef)sd).addVertex(new Vec2D((float) vec.x/2, (float) -vec.y/2));
                        }
                        else if(d.equals("DL"))
                        {
                            ((PolygonDef)sd).addVertex(new Vec2D((float) -vec.x/2, (float) -vec.y/2));
                            ((PolygonDef)sd).addVertex(new Vec2D((float) vec.x/2, (float) -vec.y/2));
                            ((PolygonDef)sd).addVertex(new Vec2D((float) vec.x/2, (float) vec.y/2));
                        }
                        else if(d.equals("UL"))
                        {
                            ((PolygonDef)sd).addVertex(new Vec2D(vec.x/2, -vec.y/2));
                            ((PolygonDef)sd).addVertex(new Vec2D(vec.x/2, vec.y/2));
                            ((PolygonDef)sd).addVertex(new Vec2D(-vec.x/2, vec.y/2));
                        }
                        else if(d.equals("UR"))
                        {
                            ((PolygonDef)sd).addVertex(new Vec2D(-vec.x/2, vec.y/2));
                            ((PolygonDef)sd).addVertex(new Vec2D(-vec.x/2, -vec.y/2));
                            ((PolygonDef)sd).addVertex(new Vec2D(vec.x/2, vec.y/2));
                        }
                        Vec2D newCentroid = PolygonShape.computeCentroid(((PolygonDef)sd).vertices);
                        
                        if(inWorld)
                        {
                            rebuildShape();
                        }
                        size = vec;
                    }
                } catch (WorldsException ex) {
                    Main.error(ex);
                }
            }
        });
        addPropChangeHandler("Platform", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(inWorld)
                    getBody().resetFiltering();
            }
        });
        sd = new PolygonDef();
        ((PolygonDef)sd).setAsBox(2.5f, 2.5f);
        sd.restitution = 0.4f;
        sd.density = 1;
        sd.friction = 0.8f;
        bd.angularDamping = 0.5f;
        if(Main.getGame().type != 0)
        {
            bd.linearDamping = 1f;
            bd.angularDamping = 1f;
        }
        bd.position = new Vec2D(0, 0);
    }
    @Override
    public void draw(Graphics g) throws WorldsException
    {
        draw(g, 0);
    }
    public void draw(Graphics g, int t) throws WorldsException
    {
        if(defaultStroke == null)
            defaultStroke = ((Graphics2D)g).getStroke();
        if(body == null || shape == null || disabled)
            return;
        int[] xPoints = new int[3];
        int[] yPoints = new int[3];
        int i = 0;
        for(Vec2D v : ((PolygonShape)shape).getVertices())
        {
            Vec2D w = body.getWorldPoint(v).worldToScreen(Main.getView());
            xPoints[i] = (int) w.x;
            yPoints[i] = (int) w.y;
            i++;
        }
        Color color = getColorProp("Color");
        if(color != null)
        {
            if(t == 0)
            {
                if(getProp("Transparency") == null)
                    setProp("Transparency", 0f, false);
                g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), (int) ((1-getFloatProp("Transparency"))*255)));
            }
            else
                g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), t));
            g.fillPolygon(xPoints, yPoints, 3);
        }
        
        if(selected == true)
            g.setColor(Color.white);
        else if(color != null)
        {
            color = color.darker();
            g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), (int) ((1-getFloatProp("Transparency"))*255)));
        }
        if(color != null && (getBooleanProp("ShowBorder") && (texture == null || showSelf)) || selected)
        {
            if(inUse)
            {
                ((Graphics2D)g).setStroke(dashedStroke);
                g.setColor(Color.black);
                g.drawPolygon(xPoints, yPoints, 3);
                ((Graphics2D)g).setStroke(defaultStroke);
            }
            else
                g.drawPolygon(xPoints, yPoints, 3);
        }
        if(label != null && !isCharacter)
        {
            drawLabel(g, null);
        }
        /*if(defaultStroke == null)
            defaultStroke = ((Graphics2D)g).getStroke();
        if(body == null || shape == null || disabled)
            return;
        int[] xv = new int[3];
        int[] yv = new int[3];
        int i = 0;
        for(Vec2 v : ((PolygonDef)sd).vertices)
        {
            Vec2D screenPos = new Vec2D(body.getWorldLocation(v)).worldToScreen(Main.getView());
            xv[i] = (int) screenPos.x;
            yv[i] = (int) screenPos.y;
            i++;
        }
        Polygon poly = new Polygon(xv, yv, 3);
        Color color = getColorProp("Color");
        if(color != null)
        {
            if(t == 0)
            {
                if(getProp("Transparency") == null)
                    setProp("Transparency", 0, false);
                g.setColor(color.toJavaColor(getFloatProp("Transparency")));
            }
            else
                g.setColor(color.toJavaColor(t/255f));
            g.drawPolygon(poly);
            if(selected == true)
                g.setColor(Color.white);
            else if(color != null)
                g.setColor(color.mul(0.7).toJavaColor(getFloatProp("Transparency")));
            if(color != null && (getBooleanProp("ShowBorder") && (texture == null || showSelf)) || selected)
            {
                Vec2D worldPos = getDrawPosition();
                Vec2D imgPos = new Vec2D(worldPos.x - size.x * 0.5, worldPos.y + size.y * 0.5).worldToScreen(Main.getView());
                Vec2D rotateCenter = worldPos.worldToScreen(Main.getView());
                Vec2D screenSize = size.toScreenUnits();
                int ix = (int) Math.round(imgPos.x);
                int iy = (int) Math.round(imgPos.y);
                int ssx = (int) Math.round(screenSize.x);
                int ssy = (int) Math.round(screenSize.y);
                if(inUse)
                {
                    ((Graphics2D)g).setStroke(dashedStroke);
                    g.setColor(Color.black);
                    g.drawPolygon(poly);
                    ((Graphics2D)g).setStroke(defaultStroke);
                }
                else
                    g.drawPolygon(poly);
            }
        }
        */
    }
}